package com.fzf.org.controller;

import com.fzf.org.dto.req.user.UserLoginReqDTO;
import com.fzf.org.dto.req.user.UserRegisterReqDTO;
import com.fzf.org.dto.req.user.UserUpdateReqDTO;
import com.fzf.org.dto.resp.user.UserSelfRespDTO;
import com.fzf.org.result.Result;
import com.fzf.org.result.Results;
import com.fzf.org.service.UserService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Size;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RequestMapping("/blog/user")
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    // todo 前端定期（比如30s）发送健康检查请求，由于会被拦截器拦截，这里的返回值需要商榷
    @GetMapping("/check/health")
    public Result<Boolean> checkAccountIfHealth() {
        return Results.success(true);
    }

    @GetMapping("/check/exist")
    public Result<Boolean> checkAccountIfExist(@Valid @Size(min = 6, max = 20) String account) {// 123
        return Results.success(userService.checkAccountIfExist(account));
    }

    @GetMapping("/check/normal")
    public Result<Boolean> checkAccountIfNormal(@Valid @Size(min = 6, max = 20) String account) {
        return Results.success(userService.checkAccountIfNormal(account));
    }

    @PostMapping("/upload")  // ok
    public Result<Void> uploadPicture(@RequestParam("pic") MultipartFile pic) {
        userService.uploadPicture(pic);
        return Results.success();
    }

    @PostMapping("/register")
    public Result<Boolean> register(@Valid @RequestBody UserRegisterReqDTO userRegisterReqDTO) {
        return Results.success(userService.register(userRegisterReqDTO));
    }

    @GetMapping
    public Result<UserSelfRespDTO> profile() {
        return Results.success(userService.profile());
    }

    @PutMapping
    public Result<Void> updateUserInfo(@Valid @RequestBody UserUpdateReqDTO userUpdateReqDTO) {
        userService.updateUserInfo(userUpdateReqDTO);
        return Results.success();
    }

    @PostMapping("/login")
    public Result<String> login(@Valid @RequestBody UserLoginReqDTO userLoginReqDTO) {
        return Results.success(userService.login(userLoginReqDTO));
    }

    @DeleteMapping("/logout")
    public Result<Void> logout() {
        userService.logout();
        return Results.success();
    }
}
